home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / win_tga.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  154 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  WIN_TGA.CPP - MS-Windows TARGA Bitmap Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/12/17 - Created.
  8. //              95/02/05 - Version 1.02A release.
  9. //              95/07/21 - Version 1.02B release.
  10. //              96/02/14 - Version 1.02C release.
  11. //              96/04/01 - Version 1.03A release.
  12. //
  13. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  14. //              Borland C++ Version 4.5
  15. //
  16. //  Author:     Ian Ashdown, P.Eng.
  17. //              byHeart Software Limited
  18. //              620 Ballantree Road
  19. //              West Vancouver, B.C.
  20. //              Canada V7S 1W3
  21. //              Tel. (604) 922-6148
  22. //              Fax. (604) 987-7621
  23. //
  24. //  Copyright 1994-1996 byHeart Software Limited
  25. //
  26. //  The following source code has been derived from:
  27. //
  28. //    Ashdown, I. 1994. Radiosity: A Programmer's
  29. //    Perspective. New York, NY: John Wiley & Sons.
  30. //
  31. //  It may be freely copied, redistributed, and/or modified
  32. //  for personal use ONLY, as long as the copyright notice
  33. //  is included with all source code files.
  34. //
  35. ////////////////////////////////////////////////////////////
  36.  
  37. #include "win_tga.h"
  38.  
  39. // Write TARGA-format file
  40. BOOL WinTarga::Write( BYTE __huge *pbm, ColorRGB *ppal,
  41.     int width, int height, int scan_width, int num_colors,
  42.     char *fname )
  43. {
  44.   int i;                        // Loop index
  45.   BYTE blue;                    // Blue color value
  46.   BYTE green;                   // Green color value
  47.   BYTE red;                     // Red color value
  48.   BYTE __huge *pbuff = pbm;     // Buffer pointer
  49.   HFILE hfile;                  // File handle
  50.   WORD num_bytes;               // Number of scan line bytes
  51.  
  52.   // Check for existing bitmap
  53.   if (pbm == NULL)
  54.     return FALSE;
  55.  
  56.   // Initialize file header
  57.   if (num_colors == 0)
  58.   {
  59.     // No palette
  60.     header.cmap_type = (BYTE) 0;
  61.     header.image_type = (BYTE) 2;
  62.     header.cmap_num = (BYTE) 0;
  63.     header.cmap_size = (BYTE) 0;
  64.     header.bpp = (BYTE) 24;
  65.   }
  66.   else
  67.   {
  68.     // Palette
  69.     header.cmap_type = (BYTE) 1;
  70.     header.image_type = (BYTE) 1;
  71.     header.cmap_size = (BYTE) 24;
  72.     header.cmap_num = (BYTE) num_colors;
  73.     header.bpp = (BYTE) 8;
  74.   }
  75.  
  76.   header.width = (WORD) width;
  77.   header.height = (WORD) height;
  78.  
  79.   // Open the file
  80.   if ((hfile = _lcreat(fname, 0)) == HFILE_ERROR)
  81.     return FALSE;
  82.  
  83.   // Write the file header (member-by-member to avoid
  84.   // structure alignment problems with Win32)
  85.   _lwrite(hfile, (LPSTR) &(header.id_len),
  86.       sizeof(header.id_len));
  87.   _lwrite(hfile, (LPSTR) &(header.cmap_type),
  88.       sizeof(header.cmap_type));
  89.   _lwrite(hfile, (LPSTR) &(header.image_type),
  90.       sizeof(header.image_type));
  91.   _lwrite(hfile, (LPSTR) &(header.cmap_start),
  92.       sizeof(header.cmap_start));
  93.   _lwrite(hfile, (LPSTR) &(header.cmap_num),
  94.       sizeof(header.cmap_num));
  95.   _lwrite(hfile, (LPSTR) &(header.cmap_size),
  96.       sizeof(header.cmap_size));
  97.   _lwrite(hfile, (LPSTR) &(header.horz_org),
  98.       sizeof(header.horz_org));
  99.   _lwrite(hfile, (LPSTR) &(header.vert_org),
  100.       sizeof(header.vert_org));
  101.   _lwrite(hfile, (LPSTR) &(header.width),
  102.       sizeof(header.width));
  103.   _lwrite(hfile, (LPSTR) &(header.height),
  104.       sizeof(header.height));
  105.   _lwrite(hfile, (LPSTR) &(header.bpp),
  106.       sizeof(header.bpp));
  107.   _lwrite(hfile, (LPSTR) &(header.desc),
  108.       sizeof(header.desc));
  109.  
  110.   if (num_colors > 0)
  111.   {
  112.     num_bytes = (WORD) width;
  113.  
  114.     // Write palette
  115.     for (i = 0; i < num_colors; i++)
  116.     {
  117.       // Get palette entry color components
  118.       blue = ppal[i].GetBlue();
  119.       green = ppal[i].GetGreen();
  120.       red = ppal[i].GetRed();
  121.  
  122.       // Write palette entry (NOTE REVERSED ORDER!)
  123.       _lwrite(hfile, (LPSTR) &blue, sizeof(BYTE));
  124.       _lwrite(hfile, (LPSTR) &green, sizeof(BYTE));
  125.       _lwrite(hfile, (LPSTR) &red, sizeof(BYTE));
  126.     }
  127.   }
  128.   else
  129.     num_bytes = (WORD) width * 3;
  130.  
  131.   // Write bitmap to file
  132.   //
  133.   // NOTES:  1. TARGA file format stores RGB triples in
  134.   //            reverse order (same as BMP file format).
  135.   //         2. TARGA file format does NOT pad scan lines
  136.   //            to DWORD boundaries.
  137.   //
  138.   for (i = 0; i < height; i++)
  139.   {
  140.     // Write scan line
  141.     if (_lwrite(hfile, (LPSTR) pbuff, num_bytes) !=
  142.         num_bytes)
  143.       return FALSE;
  144.  
  145.     // Advance buffer pointer to next scan line
  146.     pbuff += scan_width;
  147.   }
  148.  
  149.   _lclose(hfile);       // Close the file
  150.  
  151.   return TRUE;
  152. }
  153.  
  154.